package org.dq.like.util;

import lombok.extern.slf4j.Slf4j;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;

/**
 * 摸鱼办工具
 *
 * @author klh
 * @date 2023/09/11 14:45:00
 */
@Slf4j
public class MoYuBanUtil {

    /**
     * 上班时间9-18
     */
    public static int startWork = 9;
    public static int endWork = 18;

    /**
     * 获取摸鱼办消息
     */
    public static String getMoYuMsg() {
        Calendar ca = Calendar.getInstance();
        int year = ca.get(Calendar.YEAR);
        int month = ca.get(Calendar.MONTH) + 1;
        int day = ca.get(Calendar.DAY_OF_MONTH);
        int hour = ca.get(Calendar.HOUR_OF_DAY);
        int minute = ca.get(Calendar.MINUTE);
        String morningOrAfternoon = getMorningOrAfternoon(hour);
        String motto = "工作再累，一定不要忘记喝水哦！希望此刻看到消息的人可以和我一起来喝一杯水。及时排便洗手，记得关门。和我一起成为一天喝八杯水的人吧！";

        String msg = "【摸鱼办】提醒您：\n" +
                "\uD83C\uDF41今天是" + year + "年" + month + "月" + day + "日" + getDayOfWeek() + "\uD83D\uDC68\u200D\uD83D\uDCBB" + morningOrAfternoon + "好摸鱼人!\n" +
                motto + "\n" +
                "══════════\n" +
                "\uD83D\uDE87距离下班还有：" + getEndWorkMsg() + "\n" +
                "\uD83C\uDFAE距离周末(大周)还有：" + getBigWeekdayMsg() + "\n" +
                "\uD83D\uDD79\uFE0F距离周末(小周)还有：" + getLittleWeekdayMsg() + "\n" +
                "\uD83D\uDCB0距离发工资(5号)还有：" + getPaydayMsg(5) + "\n" +
                "\uD83D\uDCB0距离发工资(10号)还有：" + getPaydayMsg(10) + "\n" +
                "\uD83D\uDCB0距离发工资(11号)还有：" + getPaydayMsg(11) + "\n" +
                "\uD83D\uDCB0距离发工资(15号)还有：" + getPaydayMsg(15) + "\n" +
                "\uD83D\uDCB0距离发工资(20号)还有：" + getPaydayMsg(20) + "\n" +
                "\uD83D\uDCB0距离发工资(25号)还有：" + getPaydayMsg(25) + "\n" +
                "\uD83D\uDCB0距离中秋还有：" + getDaysLeftMsg(2023, 9, 29) + "\n" +
                "\uD83D\uDCB0距离国庆还有：" + getDaysLeftMsg(2023, 10, 1) + "\n" +
                "\uD83C\uDF41距离元旦还有：" + getDaysLeftMsg(2024, 1, 1) + "\n" +
                "\uD83C\uDFEE距离春节还有：" + getDaysLeftMsg(2024, 2, 10) + "\n" +
                "══════════\n" +
                "有事没事起身去茶水间，去厕所，去廊道走走别老在工位上坐着。上班是帮老板赚钱，摸鱼是赚老板的钱！最后，祝愿天下所有摸鱼人，都能愉快的渡过每一天";
        return msg;
    }

    private static String getMorningOrAfternoon(int hour) {
        if (hour >= 0 && hour < 11) {
            return "上午";
        } else if (hour >= 11 && hour < 13) {
            return "中午";
        } else if (hour >= 13) {
            return "下午";
        }
        return null;
    }

    /**
     * 获取今天周几
     */
    private static String getDayOfWeek() {
        Calendar calendar = Calendar.getInstance();
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        // 将Calendar的星期值转换为对应的星期名称
        String[] daysOfWeek = {"", "周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        return daysOfWeek[dayOfWeek];
    }

    public static String getEndWorkMsg() {
        // 获取当前时间
        LocalTime currentTime = LocalTime.now();
        // 下班时间
        LocalTime quittingTime = LocalTime.of(endWork, 0);
        // 计算时间差
        Duration timeDifference = Duration.between(currentTime, quittingTime);
        // 获取小时和分钟
        long hours = timeDifference.toHours();
        long minutes = timeDifference.toMinutes() - hours * 60;
        return hours + "小时" + minutes + "分钟";
    }

    /**
     * 距离大周周末的天数
     */
    public static String getBigWeekdayMsg() {
        LocalDate today = LocalDate.now();
        LocalDate nextSaturday = today.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
        // 计算日期差距
        long daysUntilSaturday = today.until(nextSaturday).getDays();
        return daysUntilSaturday + "天";
    }

    /**
     * 距离小周周末的天数
     */
    public static String getLittleWeekdayMsg() {
        LocalDate today = LocalDate.now();
        LocalDate nextSaturday = today.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        // 计算日期差距
        long daysUntilSaturday = today.until(nextSaturday).getDays();
        return daysUntilSaturday + "天";
    }

    /**
     * 距离发工资某号的天数
     */
    public static String getPaydayMsg(int day) {
        LocalDate today = LocalDate.now();
        LocalDate next10th = today.with(TemporalAdjusters.firstDayOfNextMonth()).withDayOfMonth(day);
        long days = today.until(next10th).getDays();
        return days + "天";
    }

    /**
     * 距离某天(某年某月某日)的天数
     */
    public static String getDaysLeftMsg(int year, int month, int day) {
        LocalDate midAutumnDate = LocalDate.of(year, month, day);
        LocalDate today = LocalDate.now();
        long days = ChronoUnit.DAYS.between(today, midAutumnDate);
        return days + "天";
    }

}
